package com.example.tongzhidemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt01;
    private Button bt02;
    private Button bt03;
    private Button bt04;
    private Button bt05;
    private Button bt06;
    private Button bt07;
    private Button bt08;
    private int index = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt01 = findViewById(R.id.bt_01);
        bt02 = findViewById(R.id.bt_02);
        bt03 = findViewById(R.id.bt_03);
        bt04 = findViewById(R.id.bt_04);
        bt05 = findViewById(R.id.bt_05);
        bt06 = findViewById(R.id.bt_06);
        bt07 = findViewById(R.id.bt_07);
        bt08 = findViewById(R.id.bt_08);
        bt01.setOnClickListener(this);
        bt02.setOnClickListener(this);
        bt03.setOnClickListener(this);
        bt04.setOnClickListener(this);
        bt05.setOnClickListener(this);
        bt06.setOnClickListener(this);
        bt07.setOnClickListener(this);
        bt08.setOnClickListener(this);

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_01:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("huifang");
                } else {
                    showTongzhi();
                }
                break;
            case R.id.bt_02:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("new_client");
                } else {
                    showTongzhi();
                }
                break;
            case R.id.bt_03:


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("new_dynamic");
                } else {
                    showTongzhi();
                }
                break;
            case R.id.bt_04:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("new_task");
                } else {
                    showTongzhi();
                }
                break;
            case R.id.bt_05:
                break;
            case R.id.bt_06:
                break;
            case R.id.bt_07:
                break;
            case R.id.bt_08:
                break;


        }
    }

    private void showTongzhi() {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(MainActivity.this,"")
                .setContentTitle("这是测试通知标题")  //设置标题
                .setContentText("这是测试通知内容") //设置内容
                .setWhen(System.currentTimeMillis())  //设置时间
                .setSmallIcon(R.mipmap.ic_launcher)  //设置小图标  只能使用alpha图层的图片进行设置
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))   //设置大图标
                .setAutoCancel(true)
                .setContentIntent(pi)
                .build();
        app.notificationChannelManager.notify(index, notification);
        index++;
    }


    /*
     * 简单的发送通知
     */
    @SuppressLint("NewApi")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showNotification(String qudaoid) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, qudaoid);
        builder
                .setContentTitle("title1111"+qudaoid)
                .setContentText("content1111"+qudaoid)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pi)
                .setAutoCancel(true);
        Notification notification = builder.build();
        app.notificationChannelManager.notify(index, notification);
        index++;

    }

}
